if (!window.SinoComponent)
	window.SinoComponent = {};

SinoComponent.Bpm = {
		
	ActivityDefaultName : {
		'start' : '开始节点',
		'state' : '状态节点',
		'end' : '结束节点',
		'end-cancel' : '取消节点',
		'end-error' : '错误节点',
		'decision' : '判断节点',
		'fork' : '分支节点',
		'join' : '聚合节点',
		'task' : '人工任务节点',
		'sub-process' : '子流程节点'
	},
		
	Activity : {
		'start' : {
			type:'sys:step:picture',
			imgSrc:'img/component/bpm/jpdl/start32.png',
			sWidth:48,
			sHeight:48,
			dWidth:48,
			dHeight:48,
			provider:'start'
		},
		'state' : {
			type:'sys:step:picture',
			imgSrc:'img/component/bpm/jpdl/state32.png',
			sWidth:48,
			sHeight:48,
			dWidth:48,
			dHeight:48,
			provider:'state',
			accept:'start,state,task,decision,fork,join,sub-process'
		},
		'end' : {
			type:'sys:step:picture',
			imgSrc:'img/component/bpm/jpdl/end32.png',
			sWidth:48,
			sHeight:48,
			dWidth:48,
			dHeight:48,
			accept:'state,task,decision,join,sub-process'
		},
		'end-cancel' : {
			type:'sys:step:picture',
			imgSrc:'img/component/bpm/jpdl/end-cancel32.png',
			sWidth:48,
			sHeight:48,
			dWidth:48,
			dHeight:48,
			accept:'state,task,decision,join,sub-process'
		},
		'end-error' : {
			type:'sys:step:picture',
			imgSrc:'img/component/bpm/jpdl/end-error32.png',
			sWidth:48,
			sHeight:48,
			dWidth:48,
			dHeight:48,
			accept:'state,task,decision,join,sub-process'
		},
		'decision' : {
			type:'sys:step:picture',
			imgSrc:'img/component/bpm/jpdl/decision32.png',
			sWidth:48,
			sHeight:48,
			dWidth:48,
			dHeight:48,
			provider:'decision',
			accept:'start,state,task,decision,fork,join,sub-process'
		},
		'fork' : {
			type:'sys:step:picture',
			imgSrc:'img/component/bpm/jpdl/fork32.png',
			sWidth:48,
			sHeight:48,
			dWidth:48,
			dHeight:48,
			provider:'fork',
			accept:'start,state,task,decision,fork,join,sub-process'
		},
		'join' : {
			type:'sys:step:picture',
			imgSrc:'img/component/bpm/jpdl/join32.png',
			sWidth:48,
			sHeight:48,
			dWidth:48,
			dHeight:48,
			provider:'join',
			accept:'start,state,task,decision,fork,join,sub-process'
		},
		'task' : {
			type:'sys:step:figure',
			figureType:'Rectangular',
			changable:true,
			dWidth:80,
			dHeight:48,
			provider:'task',
			accept:'start,state,task,decision,fork,join,sub-process'
		},
		'sub-process' : {
			type:'sys:step:picture',
			imgSrc:'img/component/bpm/jpdl/sub-process32.png',
			sWidth:48,
			sHeight:48,
			dWidth:48,
			dHeight:48,
			provider:'sub-process',
			accept:'start,state,task,decision,fork,join,sub-process'
		}
	},
		
	Define : {
		stepContextMenu : function(canvasObj,flowObj,stepObj,exParams){
			console.log(exParams.viewBeanId);
		},
		stepDblClick : function(canvasObj,flowObj,stepObj,exParams){
			SinoPortal.View.createWin(
				'bpm-activity-scene',
				'bpm-activity-'+stepObj.extendData.type,
				exParams.viewBeanId,
				{nodeName:stepObj.extendData.name,displayName:stepObj.extendData.name,nodeType:stepObj.extendData.type}
			);
		},
		stepMove : function(canvasObj,flowObj,hopObj,exParams){
			SinoPortal.View.modifyTabFlag(exParams.viewBeanId, true);
		},
		stepEndHop : function(canvasEls,fromElId,toElId,stateHop){
			// 不能有重复的连线
			if(canvasEls.hops && canvasEls.hops.length>0){
				for(var i=0;i<canvasEls.hops.length;i++){
					if(canvasEls.hops[i].fromElId == fromElId && canvasEls.hops[i].toElId == toElId){
						return false;
					}
				}
			}
			
			return true;
		},
		stepAcceptHop : function(stateHop,toEl){
			var text = "到"+toEl.extendData.name;
			stateHop.text = [text];
			stateHop.extendData={name:text};
		},
		hopContextMenu : function(canvasObj,flowObj,hopObj,exParams){
			console.log(flowObj);
			console.log(hopObj);
			console.log(exParams.viewBeanId);
		},
		hopDblClick : function(canvasObj,flowObj,hopObj,exParams){
			var fromEl = flowObj.getElById(hopObj.fromElId,'step');
			var toEl = flowObj.getElById(hopObj.toElId,'step');
			
			SinoPortal.View.createWin(
				'bpm-transition-scene',
				'bpm-transition-'+fromEl.el.extendData.type,
				exParams.viewBeanId,
				{
					fromActivityName:fromEl.el.extendData.name,
					fromActivityType:fromEl.el.extendData.type,
					toActivityName:toEl.el.extendData.name,
					toActivityType:toEl.el.extendData.type,
					name:hopObj.extendData.name,
					displayName:hopObj.extendData.name
				}
			);
			
		},
		hopMove : function(canvasObj,flowObj,hopObj,exParams){
			SinoPortal.View.modifyTabFlag(exParams.viewBeanId, true);
		},
		transitionOnStart : function(formId,pViewBeanId){
			// 判断重名，如果改变名称
			var initName = jQuery(PrimeFaces.escapeClientId(formId+":initName")).val();
			var name = jQuery(PrimeFaces.escapeClientId(formId+":name")).val();
			var fromActivityName = jQuery(PrimeFaces.escapeClientId(formId+":fromActivityName")).val();
			if(initName != name){
				eval("var canvasObj="+pViewBeanId+"_flowModel.flowChart;");
				if(canvasObj && canvasObj.childCanvas){
		    		var flow = canvasObj.getChildCanvasByIndex(0);
		    		var els = flow.config.canvasEls.hops;
		    		if(els){
			    		for(var i=0;i<els.length;i++){
			    			if(els[i] && els[i].extendData && els[i].extendData.name){
			    				// 名称相同，来源节点也相同，弹出框
								if(els[i].extendData.name == name){
									var fromEl = flow.getElById(els[i].fromElId,'step');
									if(fromEl.el.extendData.name == fromActivityName){
										// 弹出提示框
										SinoPortal.Confirm.show('error',null,'活动转移名称不能重复！');
										return false;
									}
								}
							}
			    		}
			    		// 更改名称
			    		for(var i=0;i<els.length;i++){
			    			if(els[i] && els[i].extendData && els[i].extendData.name){
								if(els[i].extendData.name == initName){
									els[i].extendData.name = name;
									els[i].text = [name];
									canvasObj.redraw();
									break;
								}
			    			}
			    		}
		    		}
				}
			}
			var rtn = SinoPortal.Validation.validateForm(formId);
			if(rtn){
				SinoPortal.View.modifyTabFlag(pViewBeanId, true);
			}
			return rtn;
		},
		activityOnStart : function(formId,pViewBeanId){
			// 判断重名，如果改变名称
			var initName = jQuery(PrimeFaces.escapeClientId(formId+":initName")).val();
			var name = jQuery(PrimeFaces.escapeClientId(formId+":name")).val();
			if(initName != name){
				eval("var canvasObj="+pViewBeanId+"_flowModel.flowChart;");
				if(canvasObj && canvasObj.childCanvas){
		    		var flow = canvasObj.getChildCanvasByIndex(0);
		    		var els = flow.config.canvasEls.steps;
		    		if(els){
			    		for(var i=0;i<els.length;i++){
			    			if(els[i] && els[i].extendData && els[i].extendData.name){
								if(els[i].extendData.name == name){
									// 弹出提示框
									SinoPortal.Confirm.show('error',null,'活动名称不能重复！');
									return false;
								}
							}
			    		}
			    		// 更改名称
			    		for(var i=0;i<els.length;i++){
			    			if(els[i] && els[i].extendData && els[i].extendData.name){
								if(els[i].extendData.name == initName){
									els[i].extendData.name = name;
									if(els[i].extendData.type == 'task')
										els[i].cText = name;
									canvasObj.redraw();
									break;
								}
			    			}
			    		}
		    		}
				}
			}
			var rtn = SinoPortal.Validation.validateForm(formId);
			if(rtn){
				SinoPortal.View.modifyTabFlag(pViewBeanId, true);
			}
			return rtn;
		},
		addActivity : function(event, ui){
			var tId = ui.draggable.get(0).id;
			var tIdArr = tId.split(":");
			var vbId = tIdArr[0].substring(0,tIdArr[0].length-5);
			var type = tIdArr[1];
			
			var targetDiv = jQuery(PrimeFaces.escapeClientId(vbId+"_form:"+vbId+"_flowSelectedDiv"));
			
			var x = Math.floor(event.clientX - targetDiv.offset().left);
			var y = Math.floor(event.clientY - targetDiv.offset().top);
			
			eval("var canvasObj="+vbId+"_flowModel.flowChart;");
			if(canvasObj && canvasObj.childCanvas){
	    		var flow = canvasObj.getChildCanvasByIndex(0);
	    		var config = {};
	    		jQuery.extend(config,SinoComponent.Bpm.Activity[type]);
	    		jQuery.extend(config,{dx:x,dy:y});
	    		// 名称 
	    		var newName = SinoComponent.Bpm.ActivityDefaultName[type];
	    		var newName2 = newName;
	    		var index = 1;
	    		while(SinoComponent.Bpm.Define.checkDuplicateName(flow.config.canvasEls.steps,newName2)){
	    			index = index + 1;
	    			newName2 = newName + index;
	    		}
	    		jQuery.extend(config,{extendData:{name:newName2,type:type,viewBeanId:vbId}});
	    		// 人工任务
	    		if(type == 'task'){
	    			jQuery.extend(config,{cText:[newName2]});
	    		}
	    		flow.appendEl(config,'step');
	    		canvasObj.redraw();
	    	}
		},
		checkDuplicateName : function(els,name){
			if(els){
				for(var i=0;i<els.length;i++){
					if(els[i] && els[i].extendData && els[i].extendData.name){
						if(els[i].extendData.name == name){
							return true;
						}
					}
				}
			}
			return false;
		}
		
	},
	
	openEdit : function(compId){
		var sel = SinoPortal.PageView.dataView.getSelected(compId+"_right_main_ul");
		if(sel.length==0){
			SinoPortal.Confirm.show("error",null,"请先选择一个待编辑的项目。");
			return;
		}
		var id = sel[0].id;
		var params = id.split(",");
		if(params[1] == 'res_dir'){
			SinoComponent.Bpm.openEditDir(params[0],params[2],params[3]);
		}else{
			SinoComponent.Bpm.openEditFile(params[0],params[2],params[3]);
		}
	},
	
	deleteDirOrFile : function(compId){
		var sel = SinoPortal.PageView.dataView.getSelected(compId+"_right_main_ul");
		if(sel.length==0){
			SinoPortal.Confirm.show("error",null,"请先选择一个待编辑的项目。");
			return;
		}
		var id = sel[0].id;
		var params = id.split(",");
		
		SinoPortal.Confirm.showConfirm('确认','您确定要删除项目['+params[3]+']？',compId+'_deleteSelectedNode("'+compId+','+params[2]+'")')
	},
	
	submitDeleteDirOrFile : function(compId){
		var sel = SinoPortal.PageView.dataView.getSelected(compId+"_right_main");
		eval(compId+"_deleteSelectedNode('"+sel[0].id+"');");
	},
	
	openEditDir : function(compId,nodeId,displayName){
		SinoPortal.View.createWin('bpm-scene','bpm-add-or-edit-dir',compId,{selectedId:nodeId,displayName:'编辑目录['+displayName+']'});
	},
	
	openEditFile : function(compId,nodeId,displayName){
		SinoPortal.View.createWin('bpm-scene','bpm-add-or-edit-file',compId,{selectedId:nodeId,displayName:'编辑文件['+displayName+']'});
	},
	
	openEditDefine : function(compId,type,nodeId,displayName){
		if(type == 'res_file_jpdl'){
			SinoPortal.View.createTab('bpm-scene','bpm-jpdl-define',
				compId,{selectedId:nodeId,displayName:'编辑['+displayName+']'});
		}
	},
	
	onDblClickNode : function(compId,type,nodeId,displayName){
		if(type == 'deployment' || type == 'res_dir'){
			eval(compId+"_changeCurrentNode('"+compId+","+nodeId+"');");
		}else{
			SinoComponent.Bpm.openEditDefine(compId,type,nodeId,displayName);
		}
	},
	
	changeOuterControlType : function(compId,buttonId,ct){
		var btn = jQuery(PrimeFaces.escapeClientId(buttonId));
		var ctrlType = "";
		if(btn.hasClass("ui-state-highlight")){
			btn.removeClass("ui-state-highlight");
		}else{
			ctrlType = ct;
			btn.parent().children(".ui-state-highlight").removeClass("ui-state-highlight");
			btn.addClass("ui-state-highlight");
		}
		eval("var canvasObj="+compId+"_flowModel.flowChart;");
    	if(canvasObj){
    		if(ctrlType.indexOf("inner:") == 0){
    			if(ctrlType == 'inner:dealCreateHop'){
        			var methodName = ctrlType.substring(6);
        			if(methodName == "dealCreateHop"){
        				var flow = canvasObj.getChildCanvasByIndex(0);
        				jQuery.extend(flow.config.defaultAttributes.onInitHop,{
        					arrowStartType : "",
        					arrowEndType : "default",
        					lineType : "solid",
        					onDblClick : "dblClickHop"
        				});
        			}
        		}
    		}
    		canvasObj.setOuterControl(true,ctrlType);
    		canvasObj.redraw();
    	}
	},
	
	deleteSelectedElFromOutset: function(compId){
		eval("var canvasObj="+compId+"_flowModel.flowChart;");
    	if(canvasObj && canvasObj.childCanvas){
    		var flow = canvasObj.getChildCanvasByIndex(0);
			flow.deleteSelectedEl();
    		canvasObj.redraw();
    	}
    },

    saveFromOutset : function(compId){
    	eval("var canvasObj="+compId+"_flowModel.flowChart;");
    	if(canvasObj && canvasObj.childCanvas){
    		var flow = canvasObj.getChildCanvasByIndex(0);
    		
    		var str = jQuery(PrimeFaces.escapeClientId(compId+"_form:pageCanvasString"));
    		str.val(flow.getElsValue());
    	}
    },
    
    saveFromOutsetComplete : function(compId){
    	SinoPortal.View.modifyTabFlag(compId, false);
    }
};